import Mock from 'mockjs'
import { param2Obj } from '@/utils'

const { Random } = Mock
const List = []
const count = 100


for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    timestamp: +Mock.Random.date('T'),
    author: '@cname',
    reviewer: '@cname',
    email: '@email',
    title: '@ctitle(5, 10)',
    content_short: '我是测试数据',
    content: '@cparagraph(100, 200)',
    forecast: '@float(0, 100, 2, 2)',
    importance: '@integer(1, 100)',
    'type|1': ['primary', 'success', 'warning', 'danger', 'info'],
    'status|1': ['success', 'exception', 'warning'],
    display_time: '@datetime',
    comment_disabled: true,
    pageviews: '@integer(300, 5000)',
    // image: '@Image("200x100","@color","一路向北")',
    // image: 'https://uploadbeta.com/api/pictures/random/',
    // image: 'https://source.unsplash.com/200x110/',
    image: `https://picsum.photos/200/110?random=${Math.random()}`,
    platforms: ['a-platform'],
  }))
}

export default {
  getList: (config) => {
    const {
      importance, type, title, page = 1, limit = 20, sort,
    } = param2Obj(config.url)

    let mockList = List.filter((item) => {
      if (importance && item.importance !== +importance) return false
      if (type && item.type !== type) return false
      if (title && item.title.indexOf(title) < 0) return false
      return true
    })

    if (sort === '-id') {
      mockList = mockList.reverse()
    }

    const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

    return {
      total: mockList.length,
      list: pageList,
    }
  },
  getPv: () => ({
    pvData: [{ key: 'PC', pv: 1024 }, { key: 'mobile', pv: 1024 }, { key: 'ios', pv: 1024 }, { key: 'android', pv: 1024 }],
  }),
  getArticle: (config) => {
    const { id } = param2Obj(config.url)
    for (const article of List) {
      if (article.id === +id) {
        return article
      }
    }
  },
  createArticle: () => ({
    data: 'success',
  }),
  updateArticle: () => ({
    data: 'success',
  }),
}
